package com.cmcc.aregister.ui.activity;

import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.cmcc.aregister.MyApplication;
import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.constant.Arguments;
import com.cmcc.aregister.cs.constant.ConfigUtilHelper;
import com.cmcc.aregister.ui.adapter.ImageAdapter;

/**
 * 主页面
 */
public class MainActivity extends ActivityGroup
{
    // -------view---------
    private GridView bottom_view;
    // private ImageView bottom_select_image;
    private LinearLayout container;
    // -------
    private ImageAdapter bottomAdapter;
    protected Context context;
    protected MyApplication app;
    // ---------
    private int index = 0;
    private int currentBottomPosition = -1;
    protected LocalActivityManager activityManager;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        context = this;
        app = MyApplication.getInstance();
        activityManager = getLocalActivityManager();
        setContentView(R.layout.p_main);
        setupData();
        setupView();
    }

    protected void setupData()
    {
        index = getIntent().getIntExtra(Arguments.INTENT_TO_MAIN_ID,
            R.id.register);
    }

    protected void setupView()
    {
        bottom_view = (GridView) findViewById(R.id.bottom_view);
        // bottom_select_image = (ImageView)
        // findViewById(R.id.bottm_select_image);
        container = (LinearLayout) findViewById(R.id.container);
        // gridview 改变点击背景颜色
        bottom_view.setSelector(new ColorDrawable(Color.TRANSPARENT));
        bottomAdapter = new ImageAdapter(context, 5);
        bottom_view.setAdapter(bottomAdapter);
        bottom_view.setOnItemClickListener(onItemClickListener);
        switchActivity(index);
    }

    OnItemClickListener onItemClickListener = new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View view, int position,
            long arg3)
        {
            // if (position == 2 || position == 5)
            // {
            // ConfigUtilHelper.isFromMain = true;
            // }
            switchActivity(position);
        }
    };

    /**
     * 根据ID打开指定的Activity
     * 
     * @param id GridView选中项的序号
     */
    public void switchActivity(int id)
    {
        if (id == 5)
        {// 语音挂号
            Intent intent = null;
            intent = new Intent(MainActivity.this, NewVoiceActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            Window subActivity = activityManager.startActivity("subActivity",
                intent);
            container.addView(subActivity.getDecorView(),
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
            return;
        }
        // if (index == id)
        // {

        // }
        // if (currentBottomPosition == -1) {
        // bottomAdapter.setFouse(id);
        // //bottom_select_image.setVisibility(View.GONE);
        // }
        // else
        // {

        // int fromXDelta = currentBottomPosition * main_menu_width;
        // int toXDelta = id * main_menu_width;
        // Animation animation = new TranslateAnimation(fromXDelta,
        // toXDelta, 0, 0);
        // animation.setDuration(200);
        // animation.setFillAfter(true);
        // bottom_select_image.startAnimation(animation);
        // bottom_select_image.setVisibility(View.VISIBLE);
        // }
        if (currentBottomPosition == id)
        {
            return;
        }
        bottomAdapter.setFouse(id);

        bottomAdapter.setNoSelect(currentBottomPosition);

        currentBottomPosition = id;
        container.removeAllViews();// 必须先清除容器中所有的View
        Intent intent = null;
        if (id == 0)
        {

            intent = new Intent(MainActivity.this, AppointmentActivity.class);
        }
        else if (id == 1)
        {
            intent = new Intent(MainActivity.this, HospitalActivity.class);
        }
        else if (id == 2)
        {
            intent = new Intent(MainActivity.this, MemberCenterActivity.class);
        }
        else if (id == 3)
        {
            intent = new Intent(MainActivity.this, SearchActivity.class);
        }
        else if (id == 4)
        {
            intent = new Intent(MainActivity.this, MoreActivity.class);
        }

        /*
         * else if (id == 5) { intent = new Intent(MainActivity.this,
         * VoiceActivity.class); }
         */
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Window subActivity = activityManager.startActivity("subActivity",
            intent);
        container.addView(subActivity.getDecorView(), LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT);
    }

   

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {

        if (keyCode == KeyEvent.KEYCODE_MENU)
        {
            // 在这里做你想做的事情
            // super.openOptionsMenu(); // 调用这个，就可以弹出菜单
            // /switchActivity(4);
            return true;
        }
        return super.onKeyDown(keyCode, event); // 最后，一定要做完以后返回
                                                // true，或者在弹出菜单后返回true，其他键返回super，让其他键默认

    }
}